﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Abp.Application.Services.Dto;
using Abp.Authorization;
using Abp.AutoMapper;
using Abp.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using SFGX.Mango.Authorization;
using SFGX.Mango.ExpressPersons.Dto;

namespace SFGX.Mango.ExpressPersons
{
	[AbpAuthorize(PermissionNames.Pages_ExpressPersons)]
	public class ExpressPersonAppService : MangoAppServiceBase, IExpressPersonAppService
	{
		private readonly IRepository<ExpressPerson> _expressPersonRepository;
		private readonly IExpressPersonManager  _expressPersonManager;
		public ExpressPersonAppService(IRepository<ExpressPerson>  expressPersonRepository, IExpressPersonManager expressPersonManager)
		{
			_expressPersonRepository = expressPersonRepository;
			_expressPersonManager = expressPersonManager;
		}

		public async Task BindMobile(BindMobileInput input)
		{
			var entity = new ExpressPerson {
				 AvatarUrl=input.AvatarUrl,
				  Mobile=input.Mobile,
				    NickName=input.NickName,
					 OpenId=input.OpenId,
					  WXUserInfo=input.WXUserInfo,
					   

			};
			  await _expressPersonManager.BindMobile(entity);
		}

		public async Task<ListResultDto<Dto.ExpressPersonDto>> GetListAsync()
		{
			var events = await _expressPersonRepository
				.GetAll() 
				 
				.OrderByDescending(e => e.CreationTime)
				.Take(64)
				.ToListAsync();

			return new ListResultDto<ExpressPersonDto>(events.MapTo<List<ExpressPersonDto>>());
		}

		 
	}
}
